package com.hxs.learn.action.core;

import java.util.List;

import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import com.hxs.learn.entity.core.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.service.core.SysUserRoleService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author huxinsheng
 * @date 2018/3/30 16:49
 */
@RestController
@RequestMapping("core/user/role")
public class UserRoleAction extends AbstractViewAction {
    @Autowired
    private SysUserRoleService userRoleService;

    @ApiOperation(value = "编辑用户角色页面", notes = "用户管理时显示编辑用户角色页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "String", paramType = "query"),
    })
    @GetMapping(ReqConst.REQ_MODIFY_USER_ROLE_DATA_DIALOG)
    public ModelAndView modifyUserRoleDataDialog(Model model, String id) {
        model.addAttribute(ReqConst.PARAM_ID, id);
        return createModelAndView(ViewNames.PAGE_USER_ROLE_MODIFY);
    }

    @ApiOperation(value = "加载用户角色数据", notes = "用户管理时显示加载用户角色数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("getUserRoleData")
    @ResponseBody
    public Result getMenuPermissionData(String userId) {
        List<TreeJson<Object>> roleMenuPermissionTreeJson = userRoleService.getUserRoleTreeJson(userId);
        return Result.ok(roleMenuPermissionTreeJson);
    }

    @ApiOperation(value = "修改用户角色数据", notes = "用户管理时显示修改用户角色数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID(多个以,拼接)", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyRoleMenuData(@Login LoginInfo userInfo, SysUserRole sysUserRole) {
        return userRoleService.save(userInfo, sysUserRole);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
